package com.thyme.utils;
import com.sun.org.apache.xml.internal.security.utils.Base64;

import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Map;

/**
 * @author thyme
 * @ClassName Test
 * @Description TODO
 * @Date 2019/9/16 10:53
 */
public class Test {

    public static void main(String[] args) throws Exception {
        Map<String, String> keyPair = RSAEncryptUtil.genKeyPair();
        String publicKey = keyPair.get("publicKey");
        String privateKey = keyPair.get("privateKey");
        System.out.println("公钥："+ publicKey);
        System.out.println("私钥："+ privateKey);
        RSAPublicKey rsaPublicKey = RSAEncryptUtil.transformPublicKey(publicKey);
        RSAPrivateKey rsaPrivateKey = RSAEncryptUtil.transformPrivateKey(privateKey);
        System.out.println("---------公钥加密私钥解密过程----------");
        String plainText = "测试公钥加密私钥解密";
        //公钥加密过程
        byte[] cipherData = RSAEncryptUtil.publickeyEncrypt(rsaPublicKey,plainText.getBytes());
        String cipher = Base64.encode(cipherData);
        //私钥解密过程
        byte[] res = RSAEncryptUtil.privateKeyDecrypt(rsaPrivateKey, Base64.decode(cipher));
        String restr = new String(res);
        System.out.println("原文："+ plainText);
        System.out.println("加密:" + cipher);
        System.out.println("解密："+ restr);
        System.out.println();
        System.out.println("---------私钥加密公钥钥解密过程----------");
        plainText = "测试私钥加密公钥解密";
        //私钥加密过程
        cipherData = RSAEncryptUtil.privateKeyEncrypt(rsaPrivateKey, plainText.getBytes());
        cipher = Base64.encode(cipherData);
        //公钥解密过程
        res = RSAEncryptUtil.publicKeyDecrypt(rsaPublicKey, Base64.decode(cipher));
        restr=new String(res);
        System.out.println("原文："+plainText);
        System.out.println("加密："+cipher);
        System.out.println("解密："+restr);
        System.out.println();
        System.out.println("---------私钥签名过程----------");


        String content="这是用于签名的原始字符串";
        String signstr = SignUtil.sign(content, privateKey,"utf-8");

        System.out.println("签名原串："+ content);
        System.out.println("签名串："+ signstr);
        System.out.println();
        System.out.println("---------公钥校验签名----------");
        System.out.println("签名原串："+ content);
        System.out.println("签名串："+ signstr);
        System.out.println("校验结果："+ RSASignature.doCheck(content,signstr,publicKey,"utf-8"));
        System.out.println();
    }
}
